package com.crl.ui.controls.pages
{
	import flash.display.InteractiveObject;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class PaginationControl
	{
		private var prevBtn:InteractiveObject;
		private var nextBtn:InteractiveObject;
		
		private var page:IPageable;
		private var pageList:PageList;
		public function PaginationControl()
		{
		}
		
		public function bindPage(page:IPageable):void{
			this.page=page;
			this.page.addEventListener(Event.CHANGE,pageChangeHandler);
		}
		
		public function bindButton(prev:InteractiveObject,next:InteractiveObject):void{
			prevBtn=prev;
			nextBtn=next;
			
			prevBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			nextBtn.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		public function bindPageList(pageList:PageList):void{
			this.pageList=pageList;
		}
		
		private function clickHandler(event:Event):void{
			if(this.page==null){
				throw new Error("page 未曾附值");
			}
			if(event.currentTarget==prevBtn){
				this.page.previousPage();
			}else{
				this.page.nextPage()
			}
		}
		
		private function pageChangeHandler(event:Event):void{
			if(this.prevBtn)this.prevBtn.mouseEnabled=this.page.hasPreviousPage;
			if(this.nextBtn)this.nextBtn.mouseEnabled=this.page.hasNextPage;
			
			if(this.pageList){
				this.pageList.displayList(this.page.currentPageData);
			}
		}
	}
}